package com.everest.tribalapp;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.json.JSONObject;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public class Setting extends Activity implements OnClickListener
{

	private LinearLayout home;
	private LinearLayout add;
	private LinearLayout setting;
	private LinearLayout logout;
	private TextView username;
	private TextView dat;
	private LinearLayout changepwd;
	private LinearLayout changepwd_display;
	protected int display_flag=0;
	private Button change_pwd_save;
	private EditText npwd;
	private EditText cpwd;
	public String url;
	@Override
	public void onBackPressed() 
	{
		
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Setting.this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		init();
		Calendar c = Calendar.getInstance();
		System.out.println("Current time => " + c.getTime());
		SimpleDateFormat df = new SimpleDateFormat("dd-MMM-yyyy");
		String formattedDate = df.format(c.getTime());
		dat.setText(formattedDate);
		
	}
	private void init()
	{
		setContentView(R.layout.setting);
		home=(LinearLayout)findViewById(R.id.home_home);
		add=(LinearLayout)findViewById(R.id.home_add);
		setting=(LinearLayout)findViewById(R.id.home_setting);
		logout=(LinearLayout)findViewById(R.id.home_logout);
		username=(TextView)findViewById(R.id.home_username);
		username.setText(Global.username);
		dat=(TextView)findViewById(R.id.home_date);	
		changepwd=(LinearLayout)findViewById(R.id.Setting_changepwd);
		changepwd_display=(LinearLayout)findViewById(R.id.change_pwd_display);
		change_pwd_save=(Button)findViewById(R.id.changepwd_save);
		npwd=(EditText)findViewById(R.id.old_pwd);
		cpwd=(EditText)findViewById(R.id.new_pwd);
		changepwd_display.setVisibility(8);
		changepwd.setOnClickListener(this);
		logout.setOnClickListener(this);
		add.setOnClickListener(this);
		home.setOnClickListener(this);
		change_pwd_save.setOnClickListener(this);
	}
	@SuppressLint("NewApi")
	@Override
	public void onClick(View v)
	{
		if(v.getId()==add.getId())
		{
			Intent intent1=new Intent(getApplicationContext(),Add1.class);
			startActivity(intent1);
			Setting.this.finish();
		}
		else if(v.getId()==home.getId())
		{
			Intent intent1=new Intent(getApplicationContext(),Home.class);
			startActivity(intent1);
			Setting.this.finish();
		}
		else if(v.getId()==logout.getId())
		{
			AlertDialog.Builder builder=new AlertDialog.Builder(Setting.this);
			builder.setTitle("Logout!!!")
			.setMessage("Are You Sure To Want To Logout?")
			.setPositiveButton("yes",new DialogInterface.OnClickListener()
			{
				public void onClick(DialogInterface dialog,int id)
				{
					Intent intent1=new Intent(getApplicationContext(),Login.class);
					startActivity(intent1);
					Setting.this.finish();
				}
			  }).setNegativeButton("NO",new DialogInterface.OnClickListener()
			  {
					public void onClick(DialogInterface dialog,int id) 
					{
					
					}
				  }).show();
		}
		else if(v.getId()==changepwd.getId())
		{
			if(display_flag==0)
			{
				display_flag=1;
				changepwd_display.setVisibility(0);
			}
			else if(display_flag==1)
			{
				display_flag=0;
				changepwd_display.setVisibility(8);
			}
		}
		else if(v.getId()==change_pwd_save.getId())
		{
			String p1=npwd.getText().toString().trim();
			String p2=cpwd.getText().toString().trim();
			if((!p1.isEmpty())&&(!p2.isEmpty()))
			{
				if(p1.equals(p2))
				{
					new Check().execute();
				}
				else
				{
					Toast.makeText(getApplicationContext(),"password not match",Toast.LENGTH_SHORT).show();
				}
			}
			else
			{
				Toast.makeText(getApplicationContext(),"some field missing",Toast.LENGTH_SHORT).show();
			}
		}
		
	}
	class Check extends AsyncTask{

		
		private String json;
		private JSONObject jsonobject;
		private String code;
		private String msg;
		@Override
		protected void onPreExecute() 
		{
			super.onPreExecute();
			url=Global.change_pwd_url+Global.username+"&password="+npwd;
			System.out.println(url);
		}

		@Override
		protected Object doInBackground(Object... params) {
			
			try 
			{
				json=JSONfunctions.getStringContent(url);
				jsonobject=new JSONObject(json);
				code=jsonobject.getString("code");
				msg=jsonobject.getString("msg");
			} 
			catch (Exception e) 
			{
				e.printStackTrace();
			}
			return null;
		}
		@Override
		protected void onPostExecute(Object result)
		{
			super.onPostExecute(result);
			if(code.equalsIgnoreCase("1"))
			{
				Intent intent=new Intent(getApplicationContext(),Setting.class);
				startActivity(intent);
				Setting.this.finish();
			}
			else
			{
				Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
			}
		}

	}
}
